#include <stdio.h>
#include <direct.h>
#include <io.h>
#include <atlconv.h>
#include "d2super_loader_common.h"

void set_default_command(ST_CLIENT_DATA *pstClientData, ST_CLIENT_DATA_EX *pstClientData_ex)
{
    memset(pstClientData, 0, sizeof(ST_CLIENT_DATA));
    memset(pstClientData_ex, 0, sizeof(ST_CLIENT_DATA_EX));

    strcpy(pstClientData_ex->m_acGameTitle, "D2Super_Loader");
    strcpy(pstClientData_ex->m_acLanguageMpq, "");
    pstClientData_ex->m_dwExtendMpq = 0;
    //m_aacExtendMpq
    pstClientData_ex->m_dwExtendPlugin = 0;
    //m_aacExtendPlugin
    //m_acGlobalMpqPath
    //m_acSuperModPath
}

void parse_command(int argc, char **argv, ST_CLIENT_DATA *pstClientData, ST_CLIENT_DATA_EX *pstClientData_ex,char m_acGlobalMpqPath[MAX_PATH])
{
    for (int i = 1; i < argc; ++i)
    {
        if (!_stricmp("-w", argv[i]))
        {
            pstClientData->window_mode = D2TRUE;
        }
        else if (!_stricmp("-glide", argv[i]))
        {
            pstClientData->glide_mode = D2TRUE;
        }
        else if (!_stricmp("-opengl", argv[i]))
        {
            pstClientData->opengl_mode = D2TRUE;
        }
        else if (!_stricmp("-d3d", argv[i]))
        {
            pstClientData->d3d_mode = D2TRUE;
        }
        else if (!_stricmp("-per", argv[i]))
        {
            pstClientData->perspective = D2TRUE;
        }
        else if (!_stricmp("-lq", argv[i]))
        {
            pstClientData->low_quality = D2TRUE;
        }
        else if (!_stricmp("-gamma", argv[i]) && i + 1 < argc)
        {
            pstClientData->gamma = atoi(argv[++i]);
        }
        else if (!_stricmp("-vsync", argv[i]))
        {
            pstClientData->vsync = D2TRUE;
        }
        else if (!_stricmp("-fr", argv[i]) && i + 1 < argc)
        {
            pstClientData->frame_rate = atoi(argv[++i]);
        }
        else if (!_stricmp("-joinid", argv[i]) && i + 1 < argc)
        {
            pstClientData->join_id = atoi(argv[++i]);
        }
        else if (!_stricmp("-gamename", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->game_name, argv[++i], sizeof(pstClientData->game_name) - 1);
        }
        else if (!_stricmp("-bn", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->bnet_ip, argv[++i], sizeof(pstClientData->bnet_ip) - 1);
        }
        else if (!_stricmp("-mcpip", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->mcp_ip, argv[++i], sizeof(pstClientData->mcp_ip) - 1);
        }
        else if (!_stricmp("-nopk", argv[i]))
        {
            pstClientData->no_pk = D2TRUE;
        }
        else if (!_stricmp("-openc", argv[i]))
        {
            pstClientData->open_c = D2TRUE;
        }
        else if (!_stricmp("-arena", argv[i]))
        {
            pstClientData->arena = D2TRUE;
        }
        else if (!_stricmp("-txt", argv[i]))
        {
            pstClientData->txt = D2TRUE;
        }
        else if (!_stricmp("-ama", argv[i]))
        {
            pstClientData->amazon = D2TRUE;
        }
        else if (!_stricmp("-pal", argv[i]))
        {
            pstClientData->paladin = D2TRUE;
        }
        else if (!_stricmp("-sor", argv[i]))
        {
            pstClientData->sorceress = D2TRUE;
        }
        else if (!_stricmp("-nec", argv[i]))
        {
            pstClientData->necromancer = D2TRUE;
        }
        else if (!_stricmp("-bar", argv[i]))
        {
            pstClientData->barbarian = D2TRUE;
        }
        else if (!_stricmp("-dru", argv[i]))
        {
            pstClientData->dru = D2TRUE;
        }
        else if (!_stricmp("-asn", argv[i]))
        {
            pstClientData->asn = D2TRUE;
        }
        else if (!_stricmp("-i", argv[i]))
        {
            pstClientData->invincible = D2TRUE;
        }
        else if (!_stricmp("-bnacct", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->account_name, argv[++i], sizeof(pstClientData->account_name) - 1);
        }
        else if (!_stricmp("-bnpass", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->game_pass, argv[++i], sizeof(pstClientData->game_pass) - 1);
        }
        else if (!_stricmp("-name", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->player_name, argv[++i], sizeof(pstClientData->player_name) - 1);
        }
        else if (!_stricmp("-realm", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->realm_name, argv[++i], sizeof(pstClientData->realm_name) - 1);
        }
        else if (!_stricmp("-ctemp", argv[i]) && i + 1 < argc)
        {
            pstClientData->c_temp = atoi(argv[++i]);
        }
        else if (!_stricmp("-nm", argv[i]))
        {
            pstClientData->no_monsters = D2TRUE;
        }
        else if (!_stricmp("-m", argv[i]))
        {
            pstClientData->monster_class = D2TRUE;
        }
        else if (!_stricmp("-minfo", argv[i]))
        {
            pstClientData->monster_info = D2TRUE;
        }
        else if (!_stricmp("-md", argv[i]))
        {
            pstClientData->monster_debug = D2TRUE;
        }
        else if (!_stricmp("-rare", argv[i]))
        {
            pstClientData->item_rare = D2TRUE;
        }
        else if (!_stricmp("-unique", argv[i]))
        {
            pstClientData->item_unique = D2TRUE;
        }
        else if (!_stricmp("-act", argv[i]) && i + 1 < argc)
        {
            pstClientData->act = atoi(argv[++i]);
        }
        else if (!_stricmp("-log", argv[i]))
        {
            pstClientData->log = D2TRUE;
        }
        else if (!_stricmp("-msglog", argv[i]))
        {
            pstClientData->msg_log = D2TRUE;
        }
        else if (!_stricmp("-safe", argv[i]))
        {
            pstClientData->safe_mode = D2TRUE;
        }
        else if (!_stricmp("-seed", argv[i]) && i + 1 < argc)
        {
            pstClientData->seed = atoi(argv[++i]);
        }
        else if (!_stricmp("-cheats", argv[i]))
        {
            pstClientData->cheats = D2TRUE;
        }
        else if (!_stricmp("-ns", argv[i]))
        {
            pstClientData->no_sound = D2TRUE;
        }
        else if (!_stricmp("-questall", argv[i]))
        {
            pstClientData->quests = D2TRUE;
        }
        else if (!_stricmp("-npl", argv[i]))
        {
            pstClientData->no_preload = D2TRUE;
        }
        else if (!_stricmp("-direct", argv[i]))
        {
            pstClientData->direct = D2TRUE;
        }
        else if (!_stricmp("-lem", argv[i]))
        {
            pstClientData->low_end = D2TRUE;
        }
        else if (!_stricmp("-nocompress", argv[i]))
        {
            pstClientData->no_gfx_compress = D2TRUE;
        }
        else if (!_stricmp("-gamepass", argv[i]) && i + 1 < argc)
        {
            strncpy_s(pstClientData->game_pass, argv[++i], sizeof(pstClientData->game_pass) - 1);
        }
        else if (!_stricmp("-skiptobnet", argv[i]))
        {
            pstClientData->skip_to_bnet = D2TRUE;
        }
        else if (!_stricmp("-client", argv[i]))
        {
        }
        else if (!_stricmp("-server", argv[i]))
        {
        }
        else if (!_stricmp("-launch", argv[i]))
        {
        }
        else if (!_stricmp("-notitle", argv[i]))
        {
        }
        else if (!_stricmp("-res800", argv[i]))
        {
        }
        else if (!_stricmp("-res640", argv[i]))
        {
        }
        else if (!_stricmp("-title", argv[i]) && i + 1 < argc)
        {
            strcpy(pstClientData_ex->m_acGameTitle, argv[++i]);
        }
        else if (!_stricmp("-locale", argv[i]) && i + 1 < argc)
        {
            sprintf(pstClientData_ex->m_acLanguageMpq, "Language_%s\\%s.mpq", argv[i + 1], argv[i + 1]);
            ++i;
        }
        else if (!_stricmp("-mpq", argv[i]))
        {
            while (i + 1 < argc && '-' != argv[i + 1][0] && pstClientData_ex->m_dwExtendMpq < MAX_EXTEND_MPQ)
            {
                sprintf_s(pstClientData_ex->m_aacExtendMpq[pstClientData_ex->m_dwExtendMpq++], "%s", argv[++i]);
            }
        }
        else if (!_stricmp("-plugin", argv[i]))
        {
            while (i + 1 < argc && '-' != argv[i + 1][0] && pstClientData_ex->m_dwExtendPlugin < MAX_EXTEND_PLUGIN)
            {
                sprintf_s(pstClientData_ex->m_aacExtendPlugin[pstClientData_ex->m_dwExtendPlugin++], "%s", argv[++i]);
            }
        }
        else if (!_stricmp("-mpqpath", argv[i]) && i + 1 < argc)
        {
            strcpy(m_acGlobalMpqPath, argv[++i]);
        }
        else if (!_stricmp("-dllpath", argv[i]))
        {
            SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
            while (i + 1 < argc && '-' != argv[i + 1][0])
            {
                USES_CONVERSION;
                AddDllDirectory(A2CW(argv[++i]));
            }
        }
        else if (!_stricmp("-supermodpath", argv[i]) && i + 1 < argc)
        {
            strcpy(pstClientData_ex->m_supermod_path, argv[++i]);
        }
    }
}
